package com.htu.controller;

import com.htu.constant.CResultType;
import com.htu.domain.Goods;
import com.htu.domain.Result;
import com.htu.model.IGoodsModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class GoodsController
{
    @Autowired
    private IGoodsModel model;


    @PostMapping("/shopping")
    public Result<List<Goods>> doGoodsMegs()
    {
        List<Goods> goods = model.doGoodsMegs();
        String msg = "Fail";
        int resultType = CResultType.FAIL;
        if (goods.size() != 0)
        {
            resultType = CResultType.SUCCESS;
            msg = "Success";
        }
        return new Result<List<Goods>>(resultType, msg, goods);
    }

    @PostMapping("/shopping/man")
    public Result<List<Goods>> doGoodsMan(int type)
    {
        List<Goods> goods = model.doGoodsMan(type);
        String msg = "fail";
        int resultType = CResultType.FAIL;
        if (goods.size() != 0)
        {
            resultType = CResultType.SUCCESS;
            msg = "success";
        }
        return new Result<List<Goods>>(resultType, msg, goods);
    }

    @PostMapping("/shopping/woman")
    public Result<List<Goods>> doGoodsWoman(int type)
    {
        List<Goods> goods = model.doGoodsWoman(type);
        String msg = "fail";
        int resultType = CResultType.FAIL;
        if (goods.size() != 0)
        {
            resultType = CResultType.SUCCESS;
            msg = "success";
        }
        return new Result<List<Goods>>(resultType, msg, goods);
    }

    @PostMapping("/goods/insert")
    public Result<Boolean> doGoodsInsert(Goods goods)
    {
        boolean commodity = model.doGoodsInsert(goods);
        String msg = "fail";
        int resultType = CResultType.FAIL;
        if (commodity)
        {
            resultType = CResultType.SUCCESS;
            msg = "success";
        }
        return new Result<Boolean>(resultType, msg, commodity);
    }

    @PostMapping("/goods/insert1")
    public Result<Boolean> doGoodInsert(Goods goods)
    {
        boolean commodity = model.doGoodInsert(goods);
        String msg = "fail";
        int resultType = CResultType.FAIL;
        if (commodity )
        {
            resultType = CResultType.SUCCESS;
            msg = "success";
        }
        return new Result<Boolean>(resultType, msg, commodity);
    }

    @PostMapping("/goods/delete")
    public Result<Boolean> doGoodsDelete(int id)
    {
        boolean commodity = model.doGoodsDelete(id);
        String msg = "fail";
        int resultType = CResultType.FAIL;
        if (commodity)
        {
            resultType = CResultType.SUCCESS;
            msg = "success";
        }
        return new Result<Boolean>(resultType, msg, commodity);
    }
}
